package fi.foyt.cs.controller;

import java.util.List;
import fi.foyt.cs.persistence.dao.system.ExternalLinkVerificationCodeDAO;
import fi.foyt.cs.persistence.dao.system.PropertyDAO;
import fi.foyt.cs.persistence.domainmodel.system.ExternalLinkVerificationCode;
import fi.foyt.cs.persistence.domainmodel.system.Property;
import fi.foyt.cs.persistence.domainmodel.user.User;

public class SystemController {

  public String setPropertyValue(String name, String value) {
    PropertyDAO propertyDAO = new PropertyDAO();
    
    Property property = propertyDAO.findByName(name);
    if (property == null) {
      property = propertyDAO.create(name, value);
    } else {
      property = propertyDAO.updateValue(property, value);
    }
    
    return property.getValue();
  } 
  
  public String getPropertyValue(String name) {
    PropertyDAO propertyDAO = new PropertyDAO();
    
    Property property = propertyDAO.findByName(name);
    if (property != null) {
      return property.getValue();
    }
    
    return null;
  }
  
  public Property findPropertyById(Long id) {
    PropertyDAO propertyDAO = new PropertyDAO();
    return propertyDAO.findById(id);
  }
  
  public void updatePropertyName(Property property, String name) {
    PropertyDAO propertyDAO = new PropertyDAO();
    propertyDAO.updateName(property, name);
  }
  
  public void updatePropertyValue(Property property, String value) {
    PropertyDAO propertyDAO = new PropertyDAO();
    propertyDAO.updateValue(property, value);
  }
  
  public List<Property> listProperties() {
    PropertyDAO propertyDAO = new PropertyDAO();
    return propertyDAO.listAll();
  }
  
  public ExternalLinkVerificationCode createExternalLinkVerificationCode(User user, String verificationCode, String extra) {
    ExternalLinkVerificationCodeDAO externalLinkVerificationCodeDAO = new ExternalLinkVerificationCodeDAO();
    return externalLinkVerificationCodeDAO.create(user.getId().getId(), verificationCode, extra);
  }
  
  public ExternalLinkVerificationCode findExternalLinkVerificationCodeByVerificationCode(String verificationCode) {
    ExternalLinkVerificationCodeDAO externalLinkVerificationCodeDAO = new ExternalLinkVerificationCodeDAO();
    return externalLinkVerificationCodeDAO.findByVerificationCode(verificationCode);
  }
  
  public void deleteExternalLinkVerificationCode(ExternalLinkVerificationCode externalLinkVerificationCode) {
    ExternalLinkVerificationCodeDAO externalLinkVerificationCodeDAO = new ExternalLinkVerificationCodeDAO();
    externalLinkVerificationCodeDAO.delete(externalLinkVerificationCode);
  }
}
